<template>
	<view class="j-achievement">
		<!-- 汇总 -->
		<view class="j-achievement-total">
			<view class="j-achievement-title">我的总销售业绩(元)</view>
			<view class="j-achievement-value">
				￥
				<text class="j-achievement-text">{{ allAmount }}</text>
			</view>
			<view class="j-achievement-value-referrer" v-if="userHierarchy.username">
				<view class="j-achievement-user">
					<image :src="userHierarchy.hidPic ? userHierarchy.hidPic : 'http://s499jfz9e.hb-bkt.clouddn.com/store/me/j-m-user.png'" mode=""></image>
					{{ userHierarchy.nickName }} {{ userHierarchy.username }}
				</view>
				<u-tag text="引荐人" type="info" border-color="#ffffff" size="mini" color="#ffffff" bg-color="rgba(0,0,0,0)" />
			</view>
		</view>
		<!-- 分享人业绩 -->
		<view class="j-achievement-share">
			<view class="j-achievement-s-row">
				<view class="j-achievement-s-title">直接分享人</view>
				<view class="j-achievement-s-value">
					<view class="j-achievement-s-c">
						<view class="j-achievement-s-top">
							{{ inviterNumMap.directVipNum ? inviterNumMap.directVipNum : 0 }}/{{ inviterNumMap.directNum ? inviterNumMap.directNum : 0 }}
						</view>
						<view class="j-achievement-s-bottom">智创会员/全部</view>
					</view>
					<view class="j-achievement-s-c">
						<view class="j-achievement-s-top">{{ directAmount }}</view>
						<view class="j-achievement-s-bottom">销售业绩(元)</view>
					</view>
				</view>
			</view>
			<view class="j-achievement-s-row">
				<view class="j-achievement-s-title">全部分享人</view>
				<view class="j-achievement-s-value">
					<view class="j-achievement-s-c">
						<view class="j-achievement-s-top">
							{{ inviterNumMap.indirectVipNum ? inviterNumMap.indirectVipNum : 0 }}/{{ inviterNumMap.indirectNum ? inviterNumMap.indirectNum : 0 }}
						</view>
						<view class="j-achievement-s-bottom">智创会员/全部</view>
					</view>
					<view class="j-achievement-s-c">
						<view class="j-achievement-s-top">{{ allAmount }}</view>
						<view class="j-achievement-s-bottom">部门业绩(元)</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 我的部门 -->
		<view class="j-achievement-t" v-if="directList.length">我的部门</view>
		<!-- 直推明细 -->
		<view class="j-achievement-detailed">
			<view class="j-achievement-row" v-for="(item, index) in directList" :key="index">
				<view class="j-achievement-user">
					<image :src="item.hidPic ? item.hidPic : 'http://s499jfz9e.hb-bkt.clouddn.com/store/me/j-m-user.png'" mode=""></image>
					<view class="j-achievement-info">
						<view class="j-achievement-name">
							<text>{{ item.username }}</text>
							<text class="j-achievement-time">{{ item.createTime }}</text>
						</view>
						<view class="j-achievement-nickname">{{ item.nickName }}</view>
					</view>
				</view>
				<view class="j-achievement-department">
					<view class="j-achievement-d-s">部门业绩：{{ item.communityAmount ? '￥'+item.communityAmount : '-' }}</view>
					<view class="j-achievement-d-s">
						部门构成：
						<u-tag v-if="item.communityMaxLevel == 0" text="普通用户" type="info" border-color="#EF8F06" size="mini" color="#EF8F06" bg-color="rgba(0,0,0,0)" />
						<u-tag v-if="item.communityMaxLevel == 1" text="体验员" type="info" border-color="#EF8F06" size="mini" color="#EF8F06" bg-color="rgba(0,0,0,0)" />
						<u-tag v-if="item.communityMaxLevel == 2" text="销售经理" type="info" border-color="#EF8F06" size="mini" color="#EF8F06" bg-color="rgba(0,0,0,0)" />
						<u-tag v-if="item.communityMaxLevel == 3" text="销售总监" type="info" border-color="#EF8F06" size="mini" color="#EF8F06" bg-color="rgba(0,0,0,0)" />
						<u-tag v-if="item.communityMaxLevel == 4" text="董事" type="info" border-color="#EF8F06" size="mini" color="#EF8F06" bg-color="rgba(0,0,0,0)" />
					</view>
				</view>
			</view>
		</view>
		<u-loadmore :status="loadStatus" bgColor="#f2f2f2" :margin-top="20"></u-loadmore>
	</view>
</template>

<script>
export default {
	data() {
		return {
			params: {
				page: 0,
				rows: 10
			},
			loadStatus: 'loadmore',
			allAmount: '0.00',
			directAmount: '0.00',
			inviterNumMap: {},
			userHierarchy: {},
			directList: []
		};
	},
	onLoad() {
		this.pullUp();
	},
	onPullDownRefresh() {
		this.dropDown();
	},
	onReachBottom() {
		this.pullUp();
	},
	methods: {
		// 下拉刷新
		dropDown() {
			// 修改当前view的数据
			this.params.page = 1;
			// 加载下拉刷新的数据
			this.postCommunityCount('down');
		},
		// 上拉加载
		pullUp() {
			// 增加页数
			this.params.page++;
			// 请求获取数据
			this.postCommunityCount('up');
		},
		// 用户-我的推荐-社区统计
		postCommunityCount(type) {
			this.$u.api
				.postCommunityCount(this.params)
				.then(res => {
					console.log(res, '[推广业绩页面-社区统计]');
					uni.stopPullDownRefresh();
					this.allAmount = res.data.allAmount ? res.data.allAmount : '0.00';
					this.directAmount = res.data.directAmount ? res.data.directAmount : '0.00';
					this.inviterNumMap = res.data.InviterNumMap;

					if (res.data.userHierarchy) {
						this.userHierarchy = res.data.userHierarchy;
					}

					// 如果是上拉加载
					if (type == 'up') {
						// 判断分页
						if (this.params.page <= res.data.totalPage) {
							this.directList.push(...res.data.rows);
							this.loadStatus = 'loading';

							// 如果数量小于页容量，显示没有更多数据
							if (res.data.rows.length < this.params.rows) {
								this.loadStatus = 'nomore';
							}
						} else {
							this.params.page = res.data.totalPage;
							this.loadStatus = 'nomore';
						}
					}

					// 如果是下拉刷新
					if (type == 'down') {
						// 将数据
						this.directList.push(...res.data.rows);
						this.directList.splice(0, this.directList.length == 0 ? 0 : this.directList.length - res.data.rows.length);
					}

					// this.directList = res.data.rows;
				})
				.catch(err => {
					console.log(err);
				});
		}
	}
};
</script>

<style>
page {
	background-color: #f3f3f3;
}
</style>
<style scoped lang="scss">
.j-achievement {
	padding: 25rpx;

	.j-achievement-detailed {
		.j-achievement-row {
			padding: 25rpx;
			border-radius: 14rpx;
			background-color: #ffffff;
			margin-top: 20rpx;

			&:first-of-type {
				margin-top: 0;
			}

			.j-achievement-department {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-top: 20rpx;

				.j-achievement-d-s {
					display: flex;
					align-items: center;
				}
			}

			.j-achievement-user {
				display: flex;
				align-items: center;

				image {
					width: 71rpx;
					height: 71rpx;
					border-radius: 50%;
				}

				.j-achievement-info {
					flex: 1;
					margin-left: 20rpx;

					.j-achievement-name {
						display: flex;
						align-items: center;
						justify-content: space-between;

						.j-achievement-time {
							color: #797979;
							font-size: 24rpx;
						}
					}
				}
			}
		}
	}

	.j-achievement-t {
		padding: 25rpx 15rpx;
	}

	.j-achievement-share {
		border-radius: 14rpx;
		margin-top: 20rpx;
		background-color: #ffffff;
		padding: 25rpx;

		.j-achievement-s-row {
			.j-achievement-s-value {
				display: flex;
				align-items: center;

				.j-achievement-s-c {
					flex: 1;
					min-width: 350rpx;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					margin: 30rpx 0;

					.j-achievement-s-top {
						font-size: 40rpx;
					}

					.j-achievement-s-bottom {
						color: #797979;
						font-size: 25rpx;
					}
				}
			}
		}
	}

	.j-achievement-total {
		height: 246rpx;
		display: flex;
		padding: 0 25rpx;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		background: linear-gradient(90deg, rgba(255, 132, 75, 1), rgba(255, 89, 91, 1));
		border-radius: 14rpx;

		.j-achievement-title {
			color: #ffffff;
			margin-top: 20rpx;
		}

		.j-achievement-value {
			color: #ffffff;
			font-size: 40rpx;
			font-weight: bold;

			.j-achievement-text {
				font-size: 50rpx;
			}
		}

		.j-achievement-value-referrer {
			width: 100%;
			display: flex;
			align-items: center;
			margin-top: 20rpx;
			justify-content: space-between;

			.j-achievement-user {
				color: #ffffff;
				display: flex;
				align-items: center;

				image {
					width: 42rpx;
					height: 42rpx;
					border-radius: 50%;
					margin-right: 10rpx;
				}
			}
		}
	}
}
</style>
